package peitong.peitong_oa.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import peitong.peitong_oa.domain.PageResult;
import peitong.peitong_oa.domain.QueryPageBean;
import peitong.peitong_oa.domain.Result;
import peitong.peitong_oa.domain.pojo.Staff;
import peitong.peitong_oa.domain.utils.POIUtils;
import peitong.peitong_oa.service.StaffService;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("/staff")
public class StaffController {
    @Autowired
    private StaffService staffService;
    @RequestMapping("/show")
    public PageResult findmanagepage(@RequestBody QueryPageBean queryPageBean){
        return   staffService.findpage(queryPageBean);
    }
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile file) {
        try {
            List<String[]> strings = POIUtils.readExcel(file,15);
            if(strings != null && strings.size() > 0){
                staffService.upload(strings);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,"上传错误");
        }catch (ParseException e) {
            e.printStackTrace();
            return new Result(false,"解析错误");
        }
        return new Result(true, "上传成功");
    }
    @RequestMapping("/show_staff")
    public PageResult findstaffpage(@RequestBody QueryPageBean queryPageBean){
        return   staffService.findpage_staff(queryPageBean);
    }
    @RequestMapping("/upload_staff")
    public Result uploadstaff(@RequestParam("excelFile") MultipartFile file) {
        try {
            List<String[]> strings = POIUtils.readExcel(file,15);
            if(strings != null && strings.size() > 0){
                staffService.upload_staff(strings);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,"上传错误");
        }catch (ParseException e) {
            e.printStackTrace();
            return new Result(false,"解析错误");
        }
        return new Result(true, "上传成功");
    }
    @RequestMapping("/add")
    public Result addstaff(@RequestBody Staff staff){
        try {
            staffService.addstaff(staff);
            return new Result(true,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加失败");
        }
    }
    @RequestMapping("/delete")
    public Result deletestaff(Integer id){
        try {
            staffService.deletestaff(id);
            return new Result(true,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败");
        }
    }
    @GetMapping("/getDetail")
    public Result getDetail(Integer id){

        try {
            Staff staff = staffService.findStaffDetail(id);
            return new Result(true,"获取信息成功",staff);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取信息失败");
        }
    }
}
